#!/usr/bin/env python
# encoding: utf-8
"""
rigUtils.py

Created by Fredrik Brännnbacka on 2010-11-25.
Copyright (c) 2010 Mad Crew AB. All rights reserved.
"""
from pymel.core import *

def curveToJoints(numJoints, curve=None, rebuild=True, rebuildSegments=32, basename='joint'):
    if not curve:
        try:
            curve = ls(sl=True)[0]
        except:
            print "Nothing Selected"
            return []
    if not assertType(curve, 'nurbsCurve'):
        print "Select or specifie a nurbsCurve"
        return []
    if rebuild:
        curve = rebuildCurve(curve, ch=True, rpo=False, rt=False, end=1, kr=0, kcp=0, kep=1, kt=0, s=32, d=3, tol=0)[0]
    select(cl=True)
    joints = []
    for i in range(numJoints+2):
        position = pointOnCurve( curve, pr=(1/float(numJoints)*(i)), p=True, top=True)
    	tangent = pointOnCurve( curve, pr=(1/float(numJoints)*(i)), t=True, top=True)
    	if i == numJoints+1:
        	position = [position[0]+tangent[0], position[1]+tangent[1], position[2]+tangent[2]]
        joints.append(joint(p=position, name='%s%i' % (basename,i+1)))
    for j in joints[:-1]: joint(j, edit=True, oj='xyz')
    delete(joints.pop())
    if rebuild:
	delete(curve)    
    return joints

def assertType(node, type):
    return node.getShape().type()==type